package com.nanhu.test.guesture;

import com.nanhu.test.R;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Img_control extends Activity implements OnTouchListener{
	
	private ImageView imgview;  
    private ImageView img;
    
    private Matrix matrix=new Matrix();  
    private Matrix savedMatrix=new Matrix();
    
    static final int NONE = 0;    
    static final int DRAG = 1;    
    static final int ZOOM = 2;    
    int mode = NONE;
    
    // Remember some things for zooming    
    PointF start = new PointF();    
    PointF mid = new PointF();    
    float oldDist = 1f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.img_control);
    	imgview=(ImageView)this.findViewById(R.id.imag1);
//    	imgview.setAnimation(AnimationUtils.loadAnimation(this, R.anim.newanim));
    	
    	img=(ImageView)this.findViewById(R.id.imag);
    	Matrix mt = img.getImageMatrix();
    	mt.postScale(0.5f, 0.5f);
    	mt.postScale(1.5f, 1.5f);
    	mt.postRotate(30, 130, 100);
    	mt.postTranslate(100, 10);
    	
    	//img.setImageMatrix(mt);
    	
    	imgview.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.item_mobile));
    	imgview.setOnTouchListener(this);
    	imgview.setLongClickable(true);
    }
    
    private float spacing(MotionEvent event){
    	float x = event.getX(0) - event.getX(1);
    	float y = event.getY(0) - event.getY(1);
    	return FloatMath.sqrt(x*x + y*y);
    }
    
    private void midPoint(PointF point, MotionEvent event){
    	float x = event.getX(0) + event.getX(1);
    	float y = event.getY(0) + event.getY(1);
    	point.set(x/2, y/2);
    }
    
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getActionMasked()) {
		case MotionEvent.ACTION_DOWN:
			matrix.set(imgview.getImageMatrix());
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			mode = DRAG;
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = this.spacing(event);
			if(oldDist > 10f){
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
			}
			break;
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			if(mode == DRAG){
				matrix.set(savedMatrix);
				matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
			}else if(mode == ZOOM){
				float newDist = spacing(event);
				if(newDist > 10){
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					matrix.postScale(scale, scale, mid.x, mid.y);
				}
			}
			break;
		default:
			break;
		}
		imgview.setImageMatrix(matrix);
		return false;
	}
	
}
